package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.user.constants.ApRealNameConstant;
import com.example.user.dao.ApRealNameDao;
import com.example.user.dao.ApUserDao;
import com.example.user.feign.ArticleFeign;
import com.example.user.feign.WeMediaFeign;
import com.example.user.service.ApRealNameService;
import com.usian.model.article.dtos.ApAuthorDto;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmApUserDto;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class ApRealNameServiceImpl implements ApRealNameService {

    @Autowired
    private ApRealNameDao apRealNameDao;
    @Autowired
    private ApUserDao apUserDao;
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private WeMediaFeign weMediaFeign;


    /**
     * 根据状态查询实名认证信息列表
     *
     * @param authDto
     * @return
     */
    @Override
    public List<ApUserRealname> getRealNameByParam(AuthDto authDto) {

        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //如果状态不为空，添加状态参数
        if (authDto.getStatus() != null) {
            lambdaQueryWrapper.eq(ApUserRealname::getStatus, authDto.getStatus());
        }
        log.info("请求参数authDto:{}", authDto);
        IPage<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());
        IPage<ApUserRealname> pageSensitive = apRealNameDao.selectPage(page, lambdaQueryWrapper);
        return pageSensitive.getRecords();
    }

    /**
     * 实名认证审核 0 创建中  1 待审核  2 审核失败  9 审核通过
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult check(AuthDto authDto) {
        log.info("实名认证审核,authDto:{}", authDto);
        if (authDto.getStatus() == null || authDto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //审核通过
        if (authDto.getStatus() == ApRealNameConstant.CHECK_PASS) {
            //1.修改状态(需要id)
            ApUserRealname apUserRealname = apRealNameDao.selectById(authDto.getId());
            if (apUserRealname == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }
            //修改ap_user_realname status状态
            apUserRealname.setStatus(ApRealNameConstant.CHECK_PASS);
            int i = apRealNameDao.updateById(apUserRealname);
            log.info("userId:{}", apUserRealname.getUserId());

            if (i > 0) {
                //修改成功 查询apUser表数据
                //2.通过ap_user_realname userId查询ap_user表，获取账户信息
                ApUser apUser = apUserDao.selectById(apUserRealname.getUserId());
                if (apUser == null) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
                }
                log.info("apUser:{}", apUser);
                //3.feign调用接口WemediaFeign(wemedia保存用户信息 wm_user) 判断是否重复
                ResponseResult wemediaResponseResult = weMediaFeign.getMediaUserByName(apUser.getName());
                List<WmUser> wmUserList = (List<WmUser>) wemediaResponseResult.getData();
                Integer wmUserId = null;
                //判断是否已经添加自媒体账号
                if (CollectionUtils.isEmpty(wmUserList)) {
                    WmApUserDto wmApUserDto = new WmApUserDto();
                    BeanUtils.copyProperties(apUser, wmApUserDto);
                    ResponseResult responseResult = weMediaFeign.addWeMediaUser(wmApUserDto);
                    Integer code = responseResult.getCode();
                    if (code == ApRealNameConstant.INVOKE_FAIL) {
                        return ResponseResult.errorResult(AppHttpCodeEnum.INVOKE_FAIL);
                    }
                    wmUserId = (Integer) responseResult.getData();
                }

                //4.feign调用接口ArticleFeign(article保存用户信息 ap_author) 判断重复
                ResponseResult responseResult = articleFeign.getAuthorByName(apUser.getName());
                List<ApAuthor> apAuthorList = (List<ApAuthor>) responseResult.getData();
                if (CollectionUtils.isEmpty(apAuthorList)) {
                    //保存作者信息
                    ApAuthorDto apAuthorDto = new ApAuthorDto();
                    apAuthorDto.setName(apUser.getName());
                    apAuthorDto.setUserId(apUser.getId());
                    apAuthorDto.setWmUserId(wmUserId);
                    articleFeign.addAuthor(apAuthorDto);
                }

            } else {
                log.info("修改用户实名认证状态出错");
                return ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }
}
